package com.uha.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uha.reggie.DTO.DishDto;
import com.uha.reggie.DTO.SetmealDto;
import com.uha.reggie.common.CustomException;
import com.uha.reggie.common.ReggieResult;
import com.uha.reggie.entity.Dish;
import com.uha.reggie.entity.DishFlavor;
import com.uha.reggie.entity.Setmeal;
import com.uha.reggie.entity.SetmealDish;
import com.uha.reggie.mapper.SetmealMapper;
import com.uha.reggie.service.SetmealDishService;
import com.uha.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {


    @Autowired
    private SetmealDishService setmealDishService;



    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {

        //添加套餐基本信息
        this.save(setmealDto);

        //套餐id
        Long setmealDtoId = setmealDto.getId();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{

            item.setSetmealId(setmealDtoId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional
    public SetmealDto getByIdWithDish(Long id) {

        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);


        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {

        this.updateById(setmealDto);

        //先删除 再添加
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());

            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }



    @Override
    @Transactional
    public void deleteWithDish(Long[] ids) {

            //LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
            //
            //for (Long id : ids) {
            //    queryWrapper.eq(SetmealDish::getSetmealId, id);
            //    setmealDishService.remove(queryWrapper);
            //}
            ////批量删除dish表
            //
            //this.removeByIds(Arrays.asList(ids));

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper();

        wrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus,1);

        int count = this.count(wrapper);
        if (count > 0){
            throw new CustomException("套餐停售才能删除~");
        }

        this.removeByIds(Arrays.asList(ids));

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);


    }


}
